
const mysqlConf=require('../config/mysqlConf');
const mysqlPool=require('../config/mysqlPool');
const mysql=require('mysql');
let user={
  login(req,res){
    //1.获取前台发送给服务器的值,这个是post请求,需要用body解析
    let useremail=req.body.useremail;
    let password=req.body.password;
    let userArr=[useremail,password]
    //2.去数据库进行比较
    //2.1创建数据库连接
    // let db=mysql.createConnection({
    //   host:'localhost',
    //   database:'sequelize',
    //   user:'root',
    //   password:'mWXS19971222@.',
    //   port:3306
    // })
    // //2.2打开数据库
    // db.connect()
    // //2.3操作数据库
    // db.query("select * from users where useremail=? and password=?",userArr,function (err,data) {
    //   if (data.length){
    //     res.redirect('/index.html')
    //   }else{
    //     res.redirect('/page/404.html')
    //   }
    // })
    // //2.4关闭数据库
    // db.end()
    mysqlConf.dbConfig("select * from users where useremail=? and password=?",userArr,function (err,data) {
      if (data.length){
        res.redirect('/index.html')
      }else{
        res.redirect('/page/404.html')
      }
    })
  },
  register(req,res){
    let useremail=req.body.useremail;
    let password=req.body.userpassword;
    let userArr=[password,useremail];
    // mysqlPool.dbConfig("insert into users values(null,'li','jiajia','lijiajia',?,?) ",userArr,function (err,data) {
    //   if (data){
    //     res.send('注册成功');
    //   }else{
    //     res.send('注册失败');
    //   }
    // })
    let pool=mysql.createPool({ //创造一个连接池,就会把连接自动创建出来
      host:'localhost',
      database:'sequelize',
      user:'root',
      password:'mWXS19971222@.',
      port:3306
    })
    pool.getConnection(function (err,connection) {//err获取连接对象的错误信息
      //操作数据库
      connection.query("insert into users values(null,'li','jiajia','lijiajia',?,?) ",userArr,function (err,data) {
        if (data){  //当data失败时是undefined隐式转换城boolean为false,具体验证数据库
          res.send('注册成功');
        }else{
          res.send('注册失败');
        }
      })
    });
  }
}

module.exports=user;